home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.3 / svgatext / SVGATextMode-1.3 / XFREE / common_hw / Ti3025clk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-29  |  2.0 KB  |  96 lines

  1. /* $XFree86: xc/programs/Xserver/hw/xfree86/common_hw/Ti3025clk.c,v 3.9 1995/07/01 10:49:05 dawes Exp $ */
  2.  
  3. /*
  4.  * Copyright 1994 The XFree86 Project, Inc
  5.  *
  6.  * programming the on-chip clock on the Ti3025, derived from the
  7.  * S3 gendac code by Jon Tombs
  8.  * Dirk Hohndel <hohndel@aib.com>
  9.  * Robin Cutshaw <robin@XFree86.org>
  10.  */
  11.  
  12. #include "Ti302X.h" 
  13. #include "compiler.h"
  14. #define NO_OSLIB_PROTOTYPES
  15. #include "xf86_OSlib.h"
  16. #include <math.h>
  17.  
  18.  
  19. #if NeedFunctionPrototypes
  20. void Ti3025SetClock(long freq, int clk, void (*ProgramClockFunc)())
  21. #else
  22. void
  23. Ti3025SetClock(freq, clk, ProgramClockFunc)
  24. long freq;
  25. int clk;
  26. void (*ProgramClockFunc)();
  27. #endif
  28. {
  29.    double ffreq;
  30.    int n, p, m;
  31.    int best_n=32, best_m=32;
  32.    double  diff, mindiff;
  33.    
  34. #define FREQ_MIN   27500  /* 110/8 MHz is the specified limit */
  35. #undef  FREQ_MIN
  36. #define FREQ_MIN   12000
  37. #define FREQ_MAX  220000
  38.  
  39.    if (freq < FREQ_MIN)
  40.       ffreq = FREQ_MIN / 1000.0;
  41.    else if (freq > FREQ_MAX)
  42.       ffreq = FREQ_MAX / 1000.0;
  43.    else
  44.       ffreq = freq / 1000.0;
  45.    
  46.  
  47.  
  48.    /* work out suitable timings */
  49.  
  50.    /* pick the right p value */
  51.  
  52.    for(p=0; p<4 && ffreq < 110.0; p++)
  53.       ffreq *= 2;
  54. #if FREQ_MIN < 110000/8
  55.    if (p==4) {
  56.       ffreq /= 2;
  57.       p--;
  58.    }
  59. #endif
  60.    
  61.    /* now 110.0 <= ffreq <= 220.0 */   
  62.    
  63.    ffreq /= TI_REF_FREQ;
  64.    
  65.    /* now 7.6825 <= ffreq <= 15.3650 */
  66.    /* the remaining formula is  ffreq = (m+2)*8 / (n+2) */
  67.    
  68.    
  69.    mindiff = ffreq;
  70.    
  71.    for (n = 1; n <= (int)(TI_REF_FREQ/0.5 - 2); n++) {
  72.       m = (int)(ffreq * (n+2) / 8.0 + 0.5) - 2;
  73.       if (m < 1)
  74.      m = 1;
  75.       else if (m > 127) 
  76.      m = 127;
  77.       
  78.       diff = ((m+2) * 8) / (n+2.0) - ffreq;
  79.       if (diff<0)
  80.      diff = -diff;
  81.       
  82.       if (diff < mindiff) {
  83.      mindiff = diff;
  84.      best_n = n;
  85.      best_m = m;
  86.       }
  87.    }
  88. #ifdef DEBUG
  89.    ErrorF("clk %d (%f), setting to %f, n %02x, m %02x, p %d\n", clk,freq/1e3,
  90.       8.0/(1 << p)*((best_m+2.0)/(best_n+2)) * TI_REF_FREQ,
  91.       best_n, best_m, p);
  92. #endif
  93.    
  94.    ProgramClockFunc(clk, best_n, best_m, p);
  95. }
  96.